package cn.iocoder.yudao.module.biz.controller.admin.expand.targets.customer;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.biz.controller.admin.expand.targets.customer.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.expand.targets.customer.ExpandTargetCustomerDO;
import cn.iocoder.yudao.module.biz.service.expand.targets.customer.ExpandTargetCustomerService;

@Tag(name = "管理后台 - 拓展目标_客户")
@RestController
@RequestMapping("/san/biz-expand-target-customer")
@Validated
public class ExpandTargetCustomerController {

    @Resource
    private ExpandTargetCustomerService expandTargetCustomerService;

    @PostMapping("/create")
    @Operation(summary = "创建拓展目标_客户")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-customer:create')")
    public CommonResult<Long> createExpandTargetCustomer(@Valid @RequestBody ExpandTargetCustomerSaveReqVO createReqVO) {
        return success(expandTargetCustomerService.createExpandTargetCustomer(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新拓展目标_客户")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-customer:update')")
    public CommonResult<Boolean> updateExpandTargetCustomer(@Valid @RequestBody ExpandTargetCustomerSaveReqVO updateReqVO) {
        expandTargetCustomerService.updateExpandTargetCustomer(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除拓展目标_客户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-customer:delete')")
    public CommonResult<Boolean> deleteExpandTargetCustomer(@RequestParam("id") Long id) {
        expandTargetCustomerService.deleteExpandTargetCustomer(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除拓展目标_客户")
                @PreAuthorize("@ss.hasPermission('san:biz-expand-target-customer:delete')")
    public CommonResult<Boolean> deleteExpandTargetCustomerList(@RequestParam("ids") List<Long> ids) {
        expandTargetCustomerService.deleteExpandTargetCustomerListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得拓展目标_客户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-customer:query')")
    public CommonResult<ExpandTargetCustomerRespVO> getExpandTargetCustomer(@RequestParam("id") Long id) {
        ExpandTargetCustomerDO expandTargetCustomer = expandTargetCustomerService.getExpandTargetCustomer(id);
        return success(BeanUtils.toBean(expandTargetCustomer, ExpandTargetCustomerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得拓展目标_客户分页")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-customer:query')")
    public CommonResult<PageResult<ExpandTargetCustomerRespVO>> getExpandTargetCustomerPage(@Valid ExpandTargetCustomerPageReqVO pageReqVO) {
        PageResult<ExpandTargetCustomerDO> pageResult = expandTargetCustomerService.getExpandTargetCustomerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExpandTargetCustomerRespVO.class));
    }

    @GetMapping("/year")
    @Operation(summary = "获得拓展目标_年份统计")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-customer:query')")
    public CommonResult<List<Integer>> getExpandTargetCustomerYear(@Valid ExpandTargetCustomerPageReqVO pageReqVO) {
        List<Integer> pageResult = expandTargetCustomerService.getExpandTargetCustomerYear(pageReqVO);
        return success(pageResult);
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出拓展目标_客户 Excel")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-customer:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExpandTargetCustomerExcel(@Valid ExpandTargetCustomerPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExpandTargetCustomerDO> list = expandTargetCustomerService.getExpandTargetCustomerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "拓展目标_客户.xls", "数据", ExpandTargetCustomerRespVO.class,
                        BeanUtils.toBean(list, ExpandTargetCustomerRespVO.class));
    }

    @GetMapping("/checkExistByName")
    @Operation(summary = "校验客户是否存在")
    @Parameter(name = "name", description = "客户名称", required = true, example = "客户名称")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-customer:query')")
    public CommonResult<Integer> checkExistByName(@RequestParam("name") String name) {
        Integer existFlag = expandTargetCustomerService.checkExistByName(name.trim());
        return success(existFlag);
    }

}